/**
 * Created by liangpure on 2017/2/20.
 */
import React from 'react'

/*
 * y:年     H: 小时
 * M：月     m: 分钟
 * d：几号   s: 秒
 * */
class MonthDay extends React.Component {
  render() {
    return (
      <div style={this.props.style}>
        <span>{this.parseTimestamp()}</span>
      </div>
    )
  }
  parseTimestamp = () => {
    const { timestamp, format } = this.props
    const date = new Date(timestamp)
    let dateArr = format.split('').map((str) => {
      if (/[yMdHms]/.test(str)) {
        switch (str) {
          case 'y': return `${date.getFullYear()}年`
          case 'M': return `${date.getMonth() + 1}月`
          case 'd': return `${date.getDate()}日`
          case 'H': return `${date.getHours()}`
          case 'm': return `${date.getMinutes() >= 10 ? date.getMinutes() : '0' + date.getMinutes()}`
          case 's': return `${date.getSeconds()}`
        }
      } else {
        return str
      }
    })
    return dateArr.join('')
  }
}

MonthDay.propTypes = {
  timestamp: React.PropTypes.number
}
MonthDay.defaultProps = {
  timestamp: 0,
  format: 'Md'
}
export default MonthDay
